package com.example.administrator.nurse.present;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.nurse.R;
import com.example.administrator.nurse.base.BaseActivity;
import com.example.administrator.nurse.common;
import com.example.administrator.nurse.util.passwordHelper;
import com.example.administrator.nurse.widget.LoginInputEdit;
import com.loopj.android.http.RequestParams;

/**
 * Created by Administrator on 2016/4/23.
 *
 */

public class ForgetPassActivity extends BaseActivity {

    private Button verification_btn;
    private TextView kefu;
    private LoginInputEdit account,code,password;
    private Handler handler;

    @Override
    protected int setLayotId() {
        return R.layout.forget_pass_layout;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iniiHandler();
        initView();
    }

    @Override
    protected void onFailure(int requestCode, Object object, String errorMsg) {

    }

    @Override
    protected void onSuccess(int requestCode, Object object, String response) {
        switch (requestCode){
            case Forget:
                if(response!=null){
                    if(response.trim().equals("0") || response.trim().equals("3")){
                        t("重置密码失败，请重试！");
                    }else if(response.trim().equals("1")){
                        t("重置密码成功!");
                        finish();
                    }
                }
                break;
        }
    }

    private void iniiHandler(){
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    case 1:
                        int itme = (int)msg.obj;
                        if(itme==0){
                            verification_btn.setOnClickListener(sendCodeListener);
                            verification_btn.setText("获取验证码");
                        }else {
                            verification_btn.setText("" + itme + "秒");
                        }
                        break;
                }
            }
        };
    }

    private final int Forget  = 0x12;
    private final int GetCode = 0x13;
    private final int GetKeFu = 0x14;
    private boolean isShowPw  = false;

    private void initView(){
        TextView titleTV= (TextView) findViewById(R.id.title);
        titleTV.setText(getResources().getString(R.string.forget_password_title));
        titleTV.setTextColor(getResources().getColor(R.color.nav_color));
        ImageView back= (ImageView) findViewById(R.id.back_btn);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //configCommonHeader(this, getResources().getString(R.string.forget_password_title), 0);
        final EditText editText= (EditText) f(R.id.new_pass);
        account  = (LoginInputEdit) f(R.id.phome_numbar);
        account.numberLimit = 3;
        code     = (LoginInputEdit) f(R.id.verification_code);
        code.numberLimit = 6;
        password = (LoginInputEdit) f(R.id.new_pass);
        password.numberLimit = 3;
        f(R.id.newpass_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isShowPw){
                    isShowPw = true;
                    /** 显示密码 */
                    editText.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_NORMAL);
                }else{
                    isShowPw = false;
                    /** 隐藏密码 */
                    editText.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
            }
        });
        verification_btn = (Button) f(R.id.verification_btn);
        verification_btn.setOnClickListener(sendCodeListener);
        kefu = (TextView) f(R.id.kefu);
        f(R.id.login_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(account.isOk("账号不能少于3位")){
                    if(password.isOk("密码不能少于3位")){
                        RequestParams requestParams = new RequestParams();
                        requestParams.put("account",account.getStringText());
                        requestParams.put("password", passwordHelper.MD5(password.getStringText()));
                        doPost(Forget, null, common.Host + "/Controller/funcController.php?func=forgetPwd",requestParams);
                    }
                }
            }
        });
    }

    private View.OnClickListener sendCodeListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(account.isOk("账号不能少于3位")){
                verification_btn.setOnClickListener(null);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for(int index = 60;index>=0;index--){
                            Message msg = new Message();
                            msg.what = 1;
                            msg.obj = index;
                            handler.sendMessage(msg);
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
        }
    };


}
